import ImageGrab
import win32api, win32con, win32com, win32com.client
import util
import random

class Ham:
    """Main bot driver"""
    def __init__(self, char):
        self.char = char    #pass Ham a character to work with
        self.running = 1    #used for control flow
        random.seed()       #seed the random number generator
        self.startGame = Area(75,379,368,427)   #area of the start game button
        self.shell = win32com.client.Dispatch('WScript.Shell')
    
    def send(self, keys):
        self.shell.SendKeys(keys, 0)
    
    def findPixel(self, area, color):
        px=ImageGrab.grab().load()
        for y in range(area.y1,area.y2):
            for x in range(area.x1,area.x2):
                if (px[x,y]==color):
                    return Coord(x,y)
        return Coord(-1,-1)
    
    def moveMouse(self, coord):
        win32api.SetCursorPos((coord.x,coord.y))
        util.rsleep(200,300)
    
    def moveClick(self, coord):
        self.moveMouse(coord)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,coord.x,coord.y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,coord.x,coord.y,0,0)

    def moveTo(self, leg):
        """Move to a particular location, minimally randomize travel time"""
        self.moveClick(Coord(leg.x,leg.y))
        util.rsleep(leg.delay-100,leg.delay+100)
    
    def mouseClick(self, coord):
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,coord.x,coord.y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,coord.x,coord.y,0,0)
    
    def moveClickArea(self, area):
        rCoord = Coord(util.rand(area.x1, area.x2), util.rand(area.y1, area.y2))
        win32api.SetCursorPos((rCoord.x,rCoord.y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,rCoord.x,rCoord.y,0,0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,rCoord.x,rCoord.y,0,0)
    
    def run(self):
        pass

class SarkothBot(Ham):
    def __init__(self, char):
        Ham.__init__(self, char)
        self.cellarPath.append(Leg(477,218,1600))   #first leg of journey
        self.teleportLoc = Coord(50,410)            #cursor location for teleport
        self.teleportDelay = 450                    #delay between teleport casts
        self.cellarPath.append(Leg(58,358,2600))    #just under the dank cellar
        self.dankEntrance = Coord(970,260)          #hover over dank cellar entrance
        self.signaturePxArea = Area(890,151,1033,332)
        self.openDankPxColor = util.rgb('87C2F9')
        self.leaveGameLoc = Coord(839,562)
        self.sarkothPath.append(Leg(230,844,1800))  #halfway down hallway
        self.sarkothPath.append(Leg(650,492,800))   #in front of sarkoth doorway
        self.sarkothLoc = Coord(469,191)            #on top of sarkoth
        self.pickitPath.append(Leg(0,0,850))        #beyond sarkoth
        self.pickitPath.append(Leg(850,0,850))      #right side of chest
        self.pickitPath.append(Leg(370,812,850))    #left side of chest
    
    def run(self):
        while self.running:
            #start game and move to sarkoth
            util.rsleep(4000,6000)
            self.moveClickArea(self.startGame)
            util.rsleep(7000,8000)
            self.send("4")   #precast energy shield
            util.rsleep(200,300)
            self.moveTo(self.cellarPath[0])
            self.moveMouse(self.teleportLoc)
            self.send("1")
            util.rsleep(400,500)
            self.send("1")
            util.rsleep(400,500)
            self.send("1")
            util.rsleep(300,400)
            self.moveTo(self.cellarPath[1])
            
            #check if dank cellar is open
            self.moveMouse(self.dankEntrance)
            signaturePixel = self.findPixel(self.signaturePxArea, self.openDankPxColor)
            if (signaturePixel.x == -1):
                #dank cellar didn't spawn, so leave game
                util.rsleep(500,600)
                self.send("{ESC}")
                self.moveClick(self.leaveGameLoc)
                util.rsleep(10000,11000)
                self.running = 0
                continue    #back to the top of the loop
            
            #if here, dank is open
            self.mouseClick(self.dankEntrance)
            util.rsleep(1200,1500)
            self.moveTo(self.sarkothPath[0])
            self.moveTo(self.sarkothPath[1])    #positioned in front of sarkoth
            
            self.running = 0
        
    
    cellarPath = []
    sarkothPath = []
    pickitPath = []
    
class Character:
    def __init__(self):
        pass

class Coord:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Leg(Coord):
    def __init__(self, x, y, time):
        Coord.__init__(self, x, y)
        self.delay = time

class Area:
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
    def __call__(self):
        pass

c = Character()
s = SarkothBot(c)
s.run()